package com.situ.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.entity.BeOrder;
import com.situ.entity.BeOrderuser;
import com.situ.service.BeOrderService;
import jdk.internal.org.objectweb.asm.commons.TryCatchBlockSorter;
import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
 * BeOrder的路由接口服务
 * 
 * @author 胡文
 *
 */
@Controller
public class Lw_BeOrderController {

	/** BeOrderService服务 */
	@Autowired
	private BeOrderService beOrderService;

	//根据类型查询施工竣工
	@RequestMapping(value="be__working.html")
	public String SgjgCx(@RequestParam(defaultValue ="1",required = false) int startPage ,
						 @RequestParam(defaultValue = "5",required = false) int PageSize, Map map){
		PageHelper.startPage(startPage, PageSize);
		List<BeOrder> belist=beOrderService.findType(6);
		PageInfo pageInfo = new PageInfo<BeOrder>(belist,5);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);
		map.put("BeList", belist);
		return "/be__working.html";
	}
	//雷雯：根据工单号查询施工竣工的每个客户信息
	@RequestMapping("querySgjg/{orderNo}")
	public String querySgjg(@PathVariable(name="orderNo") String orderNo,Map map){
		List<BeOrderuser> uslist=beOrderService.selectKh(orderNo);
		map.put("uslist", uslist);
		map.put("orderNo", orderNo);
		return "be__workingForm.html";
	}

	//根据类型查询通水停水
	@RequestMapping(value="be__open.html",produces = {"application/json;charset=UTF-8"})
	public String Cxshui(@RequestParam(defaultValue ="1",required = false) int startPage ,
						   @RequestParam(defaultValue = "5",required = false) int PageSize, Map map){
		PageHelper.startPage(startPage, PageSize);
		List<BeOrder> belist=beOrderService.findType(7);
		PageInfo pageInfo = new PageInfo<BeOrder>(belist,5);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);
		map.put("BeList", belist);
		return "/be__open.html";
	}
	//通水停水的：根据ID查询
	@RequestMapping(value = "queryIDshui/{id}", produces = {"application/json;charset=UTF-8"})
	public String OpenfindOne(@PathVariable(name="id") String id,Map map) {
		BeOrder obj=  beOrderService.findOne(id);
		map.put("BeOrders",obj);
		map.put("OrderNo", obj.getOrderNo());
		return "be__openForm.html";
	}
	//修改通水通水的步骤到下一步去
	@RequestMapping(value = "updatehjy")
	public  String updatehjy(String OrderNo,BeOrder obj){
		System.out.println(OrderNo);
		obj=  beOrderService.findOne(OrderNo);

		obj.setStepID(8);
		beOrderService.updateNotNullById(obj);
		return "redirect:/be__save.html";
	}

	/**
	 * 查询所有BeOrder数据的方法
	 * @param value
	 * @return
	 */

	//雷雯：工单管理查询
	@RequestMapping(value = "queryAllOrd.html")
	public String find(BeOrder value,@RequestParam(defaultValue ="1",required = false) int startPage ,
                       @RequestParam(defaultValue = "5",required = false) int PageSize,Map map) {
        PageHelper.startPage(startPage, PageSize);
	    List<BeOrder> list= beOrderService.find(value);
        PageInfo pageInfo = new PageInfo<BeOrder>(list,5);
        map.put("pageInfo", pageInfo);
        map.put("startPage", startPage);
        map.put("PageSize", PageSize);
         map.put("items",list);
         return "be_order.html";
	}

	
	/**
	 * 通过id查询BeOrder数据的方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryBeOrderById2/{id}", produces = {"application/json;charset=UTF-8"})
	public String findOne(@PathVariable(name="id") String id,Map map) {
        BeOrder obj=  beOrderService.findOne(id);
        map.put("BeOrder",obj);
        return "页面名称自己改";
	}
	//雷雯：根据类型查询档案存档
	@RequestMapping(value="be__save.html",produces = {"application/json;charset=UTF-8"})
	public String findType(@RequestParam(defaultValue ="1",required = false) int startPage ,
						   @RequestParam(defaultValue = "5",required = false) int PageSize, Map map){
		PageHelper.startPage(startPage, PageSize);
		List<BeOrder> belist=beOrderService.findType(8);
		PageInfo pageInfo = new PageInfo<BeOrder>(belist,5);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);


		map.put("BeList", belist);
		return "/be__save.html";
	}
	//雷雯：根据工单号查询多个档案存档的客户信息
	@RequestMapping("queryKh/{orderNo}")
	public String queryKh(@PathVariable(name="orderNo") String orderNo,Map map){
		List<BeOrderuser> uslist=beOrderService.selectKh(orderNo);
		map.put("uslist", uslist);
		map.put("orderNo", orderNo);

		return "be__saveForm.html";
	}
	//雷雯：高级查询
    @RequestMapping("gjcxLw")
    public String gjcxLw(BeOrder ber,@RequestParam(defaultValue ="1",required = false) int startPage ,
						 @RequestParam(defaultValue = "5",required = false) int PageSize, Map map){
		PageHelper.startPage(startPage, PageSize);
        List<BeOrder> uslist=beOrderService.findBeOrder(ber);
		PageInfo pageInfo = new PageInfo<BeOrder>(uslist,5);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);
        //uslist.forEach(s-> System.out.println(s.toString()));
        map.put("items",uslist);
        return "be_order";
    }
	/**
	 * 插入BeOrder属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "saveBeOrder2", produces = {"application/json;charset=UTF-8"})
	public String savelw(BeOrder value) {
		  beOrderService.saveNotNull(value);
        return "redirect:queryBeOrder";
	}
	
	/**
	 * 更新BeOrder属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateBeOrder2", produces = {"application/json;charset=UTF-8"})
	public String update(BeOrder value) {
		  beOrderService.updateNotNullById(value);
          return "redirect:queryBeOrder";
	}
	
	/**
	 * 通过id删除BeOrder数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "delBeOrderById2/{id}", produces = {"application/json;charset=UTF-8"})
	public String delete(@PathVariable(name="id") String id) {
		  beOrderService.deleteById(id);
          return "redirect:queryBeOrder";
	}
	//雷雯
	@RequestMapping(value = "zhzgd")
	public String zhongzhigd(){

	    return "be_abort.html";
    }
    //雷雯：使用ajsx异步更新文本框的值
    @GetMapping(value = "queryBYoderID")
    public @ResponseBody BeOrder queryBYoderID(@RequestParam("orderId") String orderId){
		try {
			BeOrder ored = beOrderService.findOne(orderId);
			return ored;
		}catch (Exception e){
			e.printStackTrace();
		}
		//System.out.println(ored.toString());
        return null;
    }
    //雷雯：修改终止工单原因
    @RequestMapping(value = "updateZzY")
    public String updateZzY(BeOrder ored){
	    ored.setStepID(-1);
        beOrderService.updateNotNullById(ored);
        return "redirect:/zhzgd";
    }
	@InitBinder  //时间类型装换器
	public void dateconvert(WebDataBinder bind){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		PropertyEditor n=new CustomDateEditor(format, true);
		bind.registerCustomEditor(Date.class, n);
	}
    //雷雯：工单报表的高级查询加分页
	@RequestMapping(value="GJcxCharge")
	public String GJcxCharge(@RequestParam(defaultValue ="1",required = false) int startPage ,
							 @RequestParam(defaultValue = "5",required = false) int PageSize,BeOrder ber, Map map){
		PageHelper.startPage(startPage, PageSize);
		System.out.println(ber.getOrderType1());
		List<Map> maps=beOrderService.CxCollect(ber);

		PageInfo pageInfo = new PageInfo<Map>(maps,5);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);
		map.put("maps", maps);
		return "be_reportMoney.html";
	}
}
